package com.samoiloff.ui.btns {
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	/**
	 * @author NM Group
	 */
	public class ListBtnController extends EventDispatcher {
		
		private static const LIST_BTN_WIDTH:int = 28;
		
		private var _mc:MovieClip;
		private var _bgMc:BtnBg;
		private var _iconMc:MovieClip;
		
		private var _hidden:Boolean;
		
		
		public function ListBtnController(mc: MovieClip, hidden:Boolean = true) {
			_mc = mc;
			_bgMc = new BtnBg();
			_mc.addChildAt(_bgMc, 0);
			_iconMc = _mc.iconMc;
			_iconMc.mouseEnabled = _iconMc.mouseChildren = false;
			
			_mc.useHandCursor = _mc.buttonMode = true;
			
			_bgMc.width = LIST_BTN_WIDTH;
			
			this.hidden = hidden;
			
			_mc.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			_mc.addEventListener(MouseEvent.MOUSE_OUT, onOut);
			_mc.addEventListener(MouseEvent.CLICK, onClick);
			_mc.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			
			
		}
		
		public function get hidden():Boolean {
			return _hidden;
		}
		public function set hidden(hidden:Boolean):void {
			_hidden = hidden;
			_iconMc.gotoAndStop(_hidden ? 1 : 2);
		}
		
		private function onMouseDown(e:MouseEvent):void {
			_bgMc.frame = 3;
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
		}

		private function onClick(e : MouseEvent) : void {
			_bgMc.frame = 2;
			dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}

		private function onOut(e : MouseEvent) : void {
			_bgMc.frame = 1;
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OUT));
		}
		
		private function onOver(e:MouseEvent) : void {
			_bgMc.frame = 2;
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OVER));
		}
		
		public function destroy():void {
			_mc.removeEventListener(MouseEvent.MOUSE_OVER, onOver);
			_mc.removeEventListener(MouseEvent.MOUSE_OUT, onOut);
			_mc.removeEventListener(MouseEvent.CLICK, onClick);
			_mc.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_mc=  null;
			_bgMc = null;
		}

		public function get mc():MovieClip {
			return _mc;
		}
		
	}
}
